<?php

namespace Addons\Wecome\Controller;
use Home\Controller\AddonsController;

class WecomeController extends AddonsController {

    public function index() {
        $con = mysql_connect("localhost", "root", "welcome1");
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("wechatshop", $con);

        $sqlres = mysql_query('select wechat_uid from user where (userlogo = "http://blog.bzxxg.cn/wp-content/uploads/2013/07/guest.png") and wechat_uid != ""');
        $token = I("token");
        $access_token = get_access_token($token);

        while ($row = mysql_fetch_assoc($sqlres, MYSQL_ASSOC)) {
            $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$row['wechat_uid']."&lang=zh_CN");
            $userInfo = json_decode($res);
de($res);
            if ($userInfo->headimgurl) {
                $userlogo = $userInfo->headimgurl;
de($userlogo);
                $res = mysql_query('update user set userlogo = "'.$userlogo.'" where wechat_uid = "'.$row['wechat_uid'].'"');
            }
de(mysql_error());
        }

        mysql_close($con);
    }
}
